package com.example.controller;

import com.example.common.Result;
import com.example.entity.Admin;
import com.example.entity.Params;
import com.example.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
//@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    /**
     * controller里的一个方法其实就是我们平时说的web项目的一个接口的入口
     * 可以在这个方法上再加上一个url
     * 也可以指定请求方式GET POST DELETE
     * @return
     */

    @PostMapping("/login")
    public Result login(@RequestBody Admin admin){
        Admin loginUser=adminService.login(admin);
        return Result.success(loginUser);
    }

    @PostMapping("/register")
    public Result register(@RequestBody Admin admin){
        adminService.add(admin);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Admin admin){
        if (admin.getId() == null) {
            adminService.add(admin);
        }else {
            adminService.update(admin);
        }

        return Result.success();
    }

    @GetMapping
    public Result findAll(){
        List<Admin> list= adminService.findAll();
        return Result.success(list);

    }
    @GetMapping("/search")
    public Result findBySearch(Params params){
        PageInfo<Admin> info =adminService.findBySearch(params);
        return Result.success(info);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        adminService.delete(id);
        return Result.success();
    }
}

